﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.AppCode.gjips;
using MixScript.BLL.MM;
using MixScript.BLL.SD;
using MixScript.BLL.WM;
namespace MixScriptWeb.Jideco.WM
{
    public partial class fMOF : cCommonWeb2, IMIxCommon
    {
        private static string sdate = "";
        private static DataTable dtGrid;
        protected void Page_Load(object sender, EventArgs e)
        {
            mn = (MainMenu2)this.Master;
            hideStatusBar();
            if (!Page.IsPostBack)
            {
                addFindClickAttribute(this, new MixScriptTB[] { txtSection }, new string[] { "SRCMODULE" });
                //txtSupCode.Tbx.Attributes.Add("readonly", "readonly");
                addRead(new MixScriptTB[] { txtMOFno,txtSection });

                MultiView1.SetActiveView(vwFilter);

            }
        }




        #region IMIxCommon Members

        public bool isvalid()
        {
            try
            {
                if (txtSection.Text.Trim() == string.Empty)
                {
                    ShowStatusBar("Section must be filled");
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion

        private void settodt(GridView grv)
        {
            try
            {
                int c = 0;

                foreach (GridViewRow gv in grv.Rows)
                {
                    TextBox tb = (TextBox)gv.Cells[1].FindControl("txtIORFNO");
                    dtGrid.Rows[c]["iorfno"] = tb.Text;

                    tb = (TextBox)gv.Cells[2].FindControl("txtPartNo");
                    dtGrid.Rows[c]["itno"] = tb.Text;
                    tb = (TextBox)gv.Cells[3].FindControl("txtPartName");
                    dtGrid.Rows[c]["itnm"] = tb.Text;
                    
                    tb = (TextBox)gv.Cells[4].FindControl("txtQty");
                    dtGrid.Rows[c]["ioqt"] = tb.Text;


                    tb = (TextBox)gv.Cells[5].FindControl("txtUom");
                    dtGrid.Rows[c]["unmsns"] = tb.Text;

                    tb = (TextBox)gv.Cells[6].FindControl("txtDesc");
                    dtGrid.Rows[c]["mofdesc"] = tb.Text;

                    tb = (TextBox)gv.Cells[6].FindControl("txtLotNo");
                    dtGrid.Rows[c]["lotno"] = tb.Text;                  


                    c += 1;
                }

                dtGrid.AcceptChanges();

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;

                Image img = (Image)e.Row.FindControl("imgSearch");
                TextBox tiorfno = (TextBox)e.Row.FindControl("txtIORFNO");
                TextBox tpartno = (TextBox)e.Row.FindControl("txtPartNo");
                TextBox tpartnm = (TextBox)e.Row.FindControl("txtPartName");
                TextBox tuom = (TextBox)e.Row.FindControl("txtUoM");
                TextBox tqty = (TextBox)e.Row.FindControl("txtQty");
                TextBox tdsc = (TextBox)e.Row.FindControl("txtDesc");
                TextBox tlt = (TextBox)e.Row.FindControl("txtLotNo");

                addRead(new TextBox[] { tiorfno, tpartno, tpartnm, tuom, tqty });

                img.Attributes.Add("onclick", "openWndChild('../popupMOF/fm-" + this.Form.ClientID + ",a-" + tpartno.ClientID + ",b-,c-,d-,e-,f-" + "" + ",g-" + toYMD( toMDY( sdate)) + ".html',480,640)");

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
             
                ImageButton imb = (ImageButton)sender;
                string s = imb.ClientID;
              //  s = s.Replace("ContentPlaceHolder1_gvPO" + ddlPOType.SelectedValue + "_imgDelete_", "");
                int i = 0;
                int.TryParse(s, out i);
                //dtPO1.Rows[i].Delete();
                //dtPO1.AcceptChanges();
                //if (ddlPOType.SelectedValue == "1")
                //{
                //    gvPO1.DataSource = dtPO1;
                //    gvPO1.DataBind();
                //}
                //else if (ddlPOType.SelectedValue == "2")
                //{
                //    gvPO2.DataSource = dtPO1;
                //    gvPO2.DataBind();
                //}
                //else if (ddlPOType.SelectedValue == "3")
                //{
                //    gvPO3.DataSource = dtPO1;
                //    gvPO3.DataBind();
                //}
                //else if (ddlPOType.SelectedValue == "4")
                //{
                //    gvPO4.DataSource = dtPO1;
                //    gvPO4.DataBind();
                //}

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        private void initdtGrid()
        {
            dtGrid = new DataTable();
            dtGrid.Columns.Add("iorfno");
            dtGrid.Columns.Add("itno");
            dtGrid.Columns.Add("itnm");
            dtGrid.Columns.Add("ioqt");
            dtGrid.Columns.Add("arndqt");
            dtGrid.Columns.Add("unmsns");
            dtGrid.Columns.Add("mofdesc");
            dtGrid.Columns.Add("lotno");



            DataRow dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dr = dtGrid.NewRow();
            dtGrid.Rows.Add(dr);
            dtGrid.AcceptChanges();

            gvMOF.DataSource = dtGrid;
            gvMOF.DataBind();
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtDate.Text.Trim() != string.Empty)
                {
                    sdate = toMDY(txtDate.Text);
                    MultiView1.SetActiveView(vwMOF);

                    initdtGrid();
                    lbldate.Text = txtDate.Text;

                }
                else
                {
                    ShowStatusBar("Please select Date!");
                }


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isvalid())
                {
                    


                    string sout = "";
                    cMOF oMof = new cMOF();
                    if (!oMof.isRecordExist("tb_mofmst", new string[] { "mofno" }, new string[] { txtMOFno.Text }))
                    {
                       
                        oMof.sSection = txtSection.Text;
                        settodt(gvMOF);

                        

                        if (oMof.saveMOF(dtGrid,  getUserLogin(),out sout))
                        {
                           // txtMOFno.Text = oMof.sMofno;
                            Session["smofno"] = oMof.sMofno;
                            string sscript = "";
                            sscript += "<script>";
                            sscript += "window.open('../print/rptid-6,smofno-" + "" + ",suppid-" + "" + ",dl.html', '_blank');";
                            //Response.Write("alert('sfdfs');");
                             sscript += "</script>";
                            //MultiView1.SetActiveView(vwFilter);
                            ClientScript.RegisterStartupScript(GetType(), "report", sscript);
                            txtMOFno.Text = oMof.sMofno;
                            ShowStatusBar("MOF Created Successfully MOF NO:" + oMof.sMofno, msgType.msgSuccess);
                        }
                        else
                        {
                            ShowStatusBar(sout);
                        }

                    }
                    else
                    {
                        ShowStatusBar("MOF Number Already Exist");
                    }

                }
                else
                {

                }

            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnCLear_Click(object sender, EventArgs e)
        {
            txtMOFno.Text = string.Empty;
            txtSection.Text = string.Empty;
            initdtGrid();
        }

        protected void btnAddNewRow_Click(object sender, EventArgs e)
        {
            try
            {
                settodt(gvMOF);
                DataRow dr = dtGrid.NewRow();
                dtGrid.Rows.Add(dr);
                gvMOF.DataSource=dtGrid;
                gvMOF.DataBind();
                
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }


    }
}
